<template>
  <div class="home">
    <el-row>
      <el-col :span="4" style="display: flex; justify-content: flex-end"
        ><el-button type="primary" style="padding: 7px 15px" @click="tianjia"
          >添加</el-button
        ></el-col
      >
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="tableData"
          style="width: 100%; margin-bottom: 20px"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="id" label="类目ID"> </el-table-column>
          <el-table-column prop="name" label="类目名"> </el-table-column>
          <el-table-column prop="iconUrl" label="类目图标">
            <template slot-scope="scope">
              <el-image
                style="width: 50px; height: 50px"
                :src="scope.row.iconUrl"
              ></el-image>
              <!-- :preview-src-list="srcList" -->
            </template>
          </el-table-column>
          <el-table-column prop="picUrl" label="类目图片">
            <template slot-scope="scope">
              <el-image
                style="width: 50px; height: 50px"
                :src="scope.row.picUrl"
              ></el-image>
              <!-- :preview-src-list="srcList" -->
            </template>
          </el-table-column>
          <el-table-column prop="keywords" label="关键字"> </el-table-column>
          <el-table-column prop="desc" label="简介"> </el-table-column>
          <el-table-column prop="level" label="级别">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.level == 'L1'">一级类目</el-tag>
              <el-tag type="info" v-if="scope.row.level == 'L2'"
                >二级类目</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                style="padding: 7px 15px"
                @click="bianji(scope.row)"
                >编辑</el-button
              >

              <el-button
                type="danger"
                size="small"
                style="padding: 7px 15px"
                @click="shanshu(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table></el-col
      >
    </el-row>
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="ruleform">
        <el-form-item label="类目名称" :label-width="formLabelWidth">
          <el-input v-model="ruleform.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="关键字" :label-width="formLabelWidth">
          <el-input v-model="ruleform.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="级别" :label-width="formLabelWidth">
          <el-select v-model="ruleform.level" placeholder="请选择活动区域">
            <el-option label="一级类目" value="L1"></el-option>
            <el-option label="二级类目" value="L2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="父类目"
          v-if="ruleform.level == 'L2'"
          :label-width="formLabelWidth"
        >
          <el-select v-model="ruleform.pid" placeholder="请选择活动区域">
            <el-option
              v-for="(item, index) in list"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类目图标" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/storage/create"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :before-upload="beforeAvatarUpload1"
          >
            <img
              v-if="ruleform.iconUrl"
              :src="ruleform.iconUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="类目图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/storage/create"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
            :before-upload="beforeAvatarUpload2"
          >
            <img v-if="ruleform.picUrl" :src="ruleform.picUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="类目简介" :label-width="formLabelWidth">
          <el-input v-model="ruleform.desc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { splmbl, splmlm, splmtj, splmsc, splmbj } from "@/api/shangchangguanli/splm";
export default {
  data() {
    return {
      tableData: [],
      list: [],
      dialogFormVisible: false,
      ruleform: {
        desc: "",
        iconUrl: "",
        keywords: "",
        level: "",
        name: "",
        picUrl: "",
        pid: 0,
      },
      formLabelWidth: "120px",
      title: "创建",
      // srcList: []
    };
  },
  methods: {
    // 添加
    tianjia() {
      this.title = "创建";
      this.dialogFormVisible = true;
    },
    // 添加
    add() {
      if (this.title == "创建") {
        splmtj(this.ruleform).then((res) => {
          // console.log(res);
          this.$notify({
            title: "成功",
            message: "添加成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.splmbl();
          this.splmlm();
        });
      } else if (this.title == "编辑") {
        splmbj(this.ruleform).then((res) => {
          this.$notify({
            title: "成功",
            message: "编辑成功",
            type: "success",
          });
          this.dialogFormVisible = false;
          this.splmbl();
          this.splmlm();
        });
      }
    },
    // 编辑
    bianji(row) {
      this.title = "编辑";
      this.dialogFormVisible = true;
      // console.log(row);
      this.ruleform = row;
    },
    // 删除
    shanshu(row) {
      console.log("删除");
      splmsc(row).then((res) => {
        console.log(res);
        this.splmbl();
        this.splmlm();
      });
    },
    // 上传类目图标
    handleAvatarSuccess1(res, file) {
      // this.ruleform.iconUrl = URL.createObjectURL(file.raw);
      this.ruleform.iconUrl = res.data.url;
    },
    beforeAvatarUpload1(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 上传类目图片
    handleAvatarSuccess2(res, file) {
      // this.ruleform.picUrl = URL.createObjectURL(file.raw);
      this.ruleform.picUrl = res.data.url;
    },
    beforeAvatarUpload2(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    splmbl() {
      splmbl().then((res) => {
        this.tableData = res.data.list;
      });
    },
    splmlm() {
      splmlm().then((res) => {
        // console.log(res);
        this.list = res.data.list;
      });
    },
  },
  mounted() {
    this.splmbl();
    this.splmlm();
  },
};
</script>

<style lang="scss" scoped>
.home::v-deep {
  padding: 20px;
  font-size: 12px !important;
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
</style>